/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.udt.generator.core;

import com.inspur.edp.cef.designtime.api.entity.ClassInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.configGenerator.ConfigClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGenerator;
import com.inspur.edp.cef.generator.coreGenerator.valueObjectGenerator.CefValueObjectCoreGenerator;
import com.inspur.edp.cef.generator.coreGenerator.valueObjectGenerator.CefValueObjectCoreGeneratorContext;
import com.inspur.edp.cef.generator.repository.RepositoryGenerator;
import com.inspur.edp.udt.generator.UdtGeneratorContext;
import com.inspur.edp.udt.generator.api.ApiHelper;
import com.inspur.edp.udt.generator.core.config.UdtConfigClassGenerator;
import com.inspur.edp.udt.generator.core.rtgenerator.manager.UdtMgrGenerator;
import com.inspur.edp.udt.generator.core.rtgenerator.repository.UdtRepositoryGenerator;

public class UdtCoreGenerator extends CefValueObjectCoreGenerator {

    private UdtGeneratorContext udtGenContext;

    public UdtCoreGenerator(UdtGeneratorContext udtGenContext) {
        super(udtGenContext);
        this.udtGenContext = udtGenContext;
    }

    private UdtCoreGenCtx getUdtCoreGenContext() {
        return (UdtCoreGenCtx) super.getContext();
    }

    @Override
    protected CefValueObjectCoreGeneratorContext createValueObjCoreGenContext() {
        UdtCoreGenCtx ctx = new UdtCoreGenCtx(udtGenContext);
        String packageName = udtGenContext.getUdt().getCoreAssemblyInfo().getAssemblyName();
        ctx.setAssemblyName(packageName);
        ctx.setNamespace(packageName);
        return ctx;
    }

//  @Override
//  protected String getPackageName() {
//    return udtGenContext.getUdt().getCoreAssemblyInfo().getAssemblyName();
//  }


    @Override
    protected void beforeGenerate() {
        super.beforeGenerate();
        SetTypes();
    }

    private void SetTypes() {
        //TODO 1906 不再需要，去掉
        //if (CefGenContext.Metadata.Content is IGSPCommonModel cm)
        //{
        //api
        ClassInfo entityInterfaceTypeClassInfo = ApiHelper
                .GetEntityInterfaceClassName(udtGenContext.getUdt());
        MediateType entityInterfaceTypeInfo = new MediateType(
                entityInterfaceTypeClassInfo.getClassNamespace(),
                entityInterfaceTypeClassInfo.getClassName());
        if (getCefGenContext().getEntityInterfaceTypes().containsKey(udtGenContext.getUdt().getCode())
                == false) {
            getCefGenContext().getEntityInterfaceTypes()
                    .put(udtGenContext.getUdt().getCode(), entityInterfaceTypeInfo);
        }
        ClassInfo entityAllInterfaceTypeClassInfo = ApiHelper
                .GetEntityAllInterfaceClassName(udtGenContext.getUdt());
        MediateType entityAllInterfaceTypeInfo = new MediateType(
                entityAllInterfaceTypeClassInfo.getClassNamespace(),
                entityAllInterfaceTypeClassInfo.getClassName());
        if (getCefGenContext().getEntityAllInterfaceTypes()
                .containsKey(udtGenContext.getUdt().getCode()) == false) {
            getCefGenContext().getEntityAllInterfaceTypes()
                    .put(udtGenContext.getUdt().getCode(), entityAllInterfaceTypeInfo);
        }
        getCefGenContext().setRootEntityInterfaceType(entityInterfaceTypeInfo);
        getCefGenContext().setRootEntityAllInterfaceType(entityAllInterfaceTypeInfo);
//		getCefGenContext().MgrInterfaceType = GetTypeFromApiAssembly(ApiHelper.GetBEMgrInterfaceClassName(udtGenContext.getUdt()));
//		getCefGenContext().MgrAllInterfaceType = GetTypeFromApiAssembly(ApiHelper.GetBEMgrAllInterfaceClassName(udtGenContext.getUdt()));

        //entity
//		ClassInfo dataInterfaceTypeClassInfo = udtGenContext.getUdt().getGeneratedEntityClassInfo();
//		getUdtCoreGenContext().getCefGenContext().setRootDataInterfaceType(dataInterfaceType);
//		getUdtCoreGenContext().getCefGenContext().setRootNodeCode(udtGenContext.getUdt().getCode());
//		if (getCefGenContext().getDataInterfaceTypes().containsKey(udtGenContext.getUdt().getCode()) == false)
//		{
//			getCefGenContext().getDataInterfaceTypes().put(udtGenContext.getUdt().getCode(), dataInterfaceType);
//		}
        //Context.CefGenContext.ChangeSerializerType = GetTypeFromApiAssembly(ApiHelper.GetDataTypeSerializerItemClassName(udtGenContext.Udt));
        //SetNodeConstsTypes();
//		SetAssociationAndEnumClassTypes();
        //}
    }

//	private void SetAssociationAndEnumClassTypes()
//	{
//		//TODO 1906 不再需要，去掉
//		for (IGspCommonField field : udtGenContext.getUdt().getContainElements())
//		{
//			UdtElement item=(UdtElement)field;
//			if (item.getIsUdt())
//			{
//				continue;
//			}
//			switch (item.getObjectType())
//			{
//				case Association:
//					if (item.getChildAssociations() != null && item.getChildAssociations().size() > 0 && getCefGenContext().getElementEnumTypes().containsKey(item.getID()) == false)
//					{
//						getCefGenContext().getElementAssociationTypes().put(item.item.getID(), GetTypeFromApiAssembly(ApiHelper.GetElementAssoClassName(item)));
//					}
//					break;
//				case Enum:
//					if (getCefGenContext().getElementEnumTypes().containsKey(item.getID()) == false)
//					{
//						getCefGenContext().getElementEnumTypes().put(item.getID(), GetTypeFromApiAssembly(ApiHelper.GetElementEnumClassName(item)));
//					}
//					break;
//				default:
//					break;
//			}
//		}
//	}

    //private void SetNodeConstsTypes()
    //{
    //    var objs = commonModel.GetAllObjectList();
    //    foreach (var item in objs)
    //    {
    //        cefGenContext.NodeConstTypes.Add(item.Code, GetTypeFromApiAssembly(ApiHelper.GetDataTypeConstClassName(item)));
    //    }
    //}

    //C# TO JAVA CONVERTER TODO TASK: Lambda expressions and anonymous methods are not converted by C# to Java Converter:
//	private java.lang.Class GetTypeFromApiAssembly(ClassInfo classInfo) => udtGenContext.ProjectApiAssembly.GetType(classInfo.GetClassFullName());
    @Override
    protected boolean getHasConfigGenerator() {
        return true;
    }

    @Override
    protected ConfigClassGenerator getConfigGenerator() {
        return new UdtConfigClassGenerator(getUdtCoreGenContext());
    }

    @Override
    protected ManagerGenerator createMgrImplGenerator() {
        return new UdtMgrGenerator(getUdtCoreGenContext());
    }

    @Override
    protected boolean getHasRepository() {
        return true;
    }


    @Override
    protected RepositoryGenerator getRepositoryGenerator() {
        return new UdtRepositoryGenerator(getContext());
    }

//  private UdtGenCtx getUdtGenContext(){
//    return new UdtGenCtx(udtGenContext);
//  };

    //  @Override
//  protected RepositoryGenerator getRepositoryGenerator() {
//    return new UdtRepoGe(getUdtCoreGenContext().getUdtGenContext());
//  }
}
